﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using PhoneStore.Entity;

namespace PhoneStore.Presentation
{
    /// <summary>
    /// Interaction logic for Manage.xaml
    /// </summary>
    public partial class Manage : WindowBase
    {
        public Manage()
        {
            InitializeComponent();
            ucPhoneList.SetUpScreenMode(ScreenMode.Manage);
            ucAccessList.SetUpScreenMode(ScreenMode.Manage);
            this.Closing += new System.ComponentModel.CancelEventHandler(Manage_Closing);
            cbbYear.ItemsSource = DAO.GetAll<Bill>().Select(x => x.SellDate.Value.Year).Distinct();
            SetUpRateComboBox();
        }

        void Manage_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Welcome w = new Welcome();
            w.Show();
        }

        private void btnTK_Click(object sender, RoutedEventArgs e)
        {
            if (cbbYear.SelectedItem == null)
            {
                MessageBox.Show("Vui Lòng Chọn Năm");
            }
            else
            {
                Graph g = new Graph(int.Parse(cbbYear.SelectedItem.ToString()));
                g.Show();
            }
        }

        private void SetUpRateComboBox()
        {
            cbbTTRate.ItemsSource = Enumerable.Range(0, 101).Select(x => new { Value = x, Text = x.ToString() + "%" });
            cbbTTRate.DisplayMemberPath = "Text";
            cbbTTRate.SelectedValuePath = "Value";
            cbbTTRate.SelectedValue = DAO.Single<Kind>(x => x.KindID == 1).rate;

            cbbVIPRate.ItemsSource = Enumerable.Range(0, 100).Select(x => new { Value = x, Text = x.ToString() + "%" });
            cbbVIPRate.DisplayMemberPath = "Text";
            cbbVIPRate.SelectedValuePath = "Value";
            cbbVIPRate.SelectedValue = DAO.Single<Kind>(x => x.KindID == 2).rate;

            cbbTTRate.IsEnabled = false;
            cbbVIPRate.IsEnabled = false;
        }

        private void btnChangeRate_Click(object sender, RoutedEventArgs e)
        {
            if (btnChangeRate.Content.ToString() == "Sửa Tỉ Lệ")
            {
                btnChangeRate.Content = "Lưu";
                cbbTTRate.IsEnabled = true;
                cbbVIPRate.IsEnabled = true;
            }
            else if (btnChangeRate.Content.ToString() == "Lưu")
            {                
                var TT = DAO.Single<Kind>(x => x.KindID == 1);
                TT.rate = double.Parse(cbbTTRate.SelectedValue.ToString());
                var VIP = DAO.Single<Kind>(x => x.KindID == 2);
                VIP.rate = double.Parse(cbbVIPRate.SelectedValue.ToString());
                DAO.Update(TT);
                DAO.Update(VIP);
                btnChangeRate.Content = "Sửa Tỉ Lệ";
                cbbTTRate.IsEnabled = false;
                cbbVIPRate.IsEnabled = false;
            }
        }
    }
}
